package chatRoom;

import java.io.IOException;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;
import java.net.SocketException;
import java.util.Scanner;

public class Sender implements Runnable{
    DatagramSocket socket=null;

    private int port;
    private String serverIp;

    public Sender(int port, String serverIp) throws SocketException {
        this.socket =new DatagramSocket();
        this.port = port;
        this.serverIp = serverIp;
    }

    @Override
    public void run() {
        Scanner in=new Scanner(System.in);
        while (true){
            try {
                DatagramPacket requestpacket=new DatagramPacket(new byte[4096],4096, InetAddress.getByName(serverIp),port);
                System.out.println("请输入你要发送的内容");
                String str=in.nextLine();
                if (str.equals("bye"))break;
                requestpacket.setData(str.getBytes());
                socket.send(requestpacket);
            } catch (IOException e) {
                throw new RuntimeException(e);
            }
        }
    }
}
